import request from '@/api/request'
import type { LoginRequest, LoginResponse, RegisterRequest, User, ApiResponse } from '@/types'

// 用户登录
export function login(data: LoginRequest): Promise<ApiResponse<LoginResponse>> {
  return request.post('/auth/login', data)
}

// 用户注册
export function register(data: RegisterRequest): Promise<ApiResponse<User>> {
  return request.post('/auth/register', data)
}

// 获取当前用户信息
export function getCurrentUser(): Promise<ApiResponse<User>> {
  return request.get('/auth/me')
}

// 用户登出
export function logout(): Promise<ApiResponse<void>> {
  return request.post('/auth/logout')
}

// 设置用户在线状态
export function setUserOnline(userId: number, sessionId: string, clientInfo: any): Promise<ApiResponse<void>> {
  return request.post('/auth/set-online', {
    userId,
    sessionId,
    clientInfo
  })
}

// 发送密码重置验证码
export function sendResetCode(email: string): Promise<ApiResponse<void>> {
  return request.post('/auth/send-reset-code', { email })
}

// 验证密码重置验证码
export function verifyResetCode(email: string, code: string): Promise<ApiResponse<void>> {
  return request.post('/auth/verify-reset-code', { email, code })
}

// 重置密码
export function resetPassword(email: string, code: string, password: string): Promise<ApiResponse<void>> {
  return request.post('/auth/reset-password', { email, code, password })
}

// 刷新token
export function refreshToken(): Promise<ApiResponse<{ token: string }>> {
  return request.post('/auth/refresh')
}

// 上传用户头像
export function uploadAvatar(file: File): Promise<ApiResponse<string>> {
  const formData = new FormData()
  formData.append('file', file)

  return request.post('/auth/profile/avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}
